import requests

#地理编码
def geocode(key,address=None,city=None,batch=None,sig=None,output=None,callback=None):
    """查找地理编码"""
    geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    wing_key = "bb6b5e6a86ad94a2b119b988e9205f0c"
    geocode_params = {
        "key":key,
        "address":address,
        "city":city,
        "batch":batch,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r = requests.get(url=geocode_url,params=geocode_params)
    return r.json()

# 逆地理编码
def regeocode(key,location=None,poitype=None,radius=None,extensions=None,batch=None,roadlevel=None,sig=None,output=None,callback=None,homeorcorp=None,):
    """地理编码查找地址"""
    regeocode_url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    regeocode_params = {
        "key":key,
        "location":location,
        "poitype":poitype,
        "radius":radius,
        "extensions":extensions,
        "batch":batch,
        "roadlevel":roadlevel,
        "sig":sig,
        "output":output,
        "callback":callback,
        "homeorcorp":homeorcorp,
    }
    r = requests.get(url=regeocode_url,params=regeocode_params)
    return r.json()

#步行路径规划
def direction(key,origin=None,destination=None,sig=None,output=None,callback=None):
    """步行路径规划"""
    direction_url = "https://restapi.amap.com/v3/direction/walking?parameters"
    direction_params = {
    "key":key,
    "origin": origin,
    "destination":destination,
    "sig":sig,
    "output":output,
    "callback":callback,
    }
    r = requests.get(url=direction_url,params=direction_params)
    return r.json()

#公交路径规划
def bustransit(key,origin,destination,city=None,cityd=None,extensions=None,strategy=None,nightflag=None,date=None,time=None,sig=None,output=None,callback=None):
    """公交路径查询"""
    publictransit_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params_publictransit = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "city":city,
        "cityd":cityd,
        "extensions":extensions,
        "strategy":strategy,
        "nightflag":nightflag,
        "date":date,
        "time":time,
        "sig":sig,
        "output":output,
        "callback":callback,
    }
    r = requests.get(url=publictransit_url,params=params_publictransit)
    return r.json()

#驾车路径规划
def driving(key,origin,destination,originid=None,destinationid=None,origintype=None,destinationtype=None,strategy=0,waypoints=None,avoidpolygons=None,avoidroad=None,province=None,number=None,cartype=0,ferry=0,roadaggregation="false",nosteps=0,sig=None,output="JSON",callback=None,extensions="base"):
    """驾车路线规划"""
    driving_url = "https://restapi.amap.com/v3/direction/driving?parameters"
    driving_params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "originid":originid,
        "destinationid":destinationid,
        "origintype":origintype,
        "destinationtype":destinationtype,
        "strategy":strategy,
        "waypoints":waypoints,
        "avoidpolygons":avoidpolygons,
        "avoidroad":avoidroad,
        "province":province,
        "number":number,
        "cartype":cartype,
        "ferry":ferry,
        "roadaggregation":roadaggregation,
        "nostops":nosteps,
        "sig":sig,
        "output":output,
        "callback":callback,
        "extensions":extensions
    }
    r = requests.get(url=driving_url,params=driving_params)
    return r.json()

#骑行路径规划
def cycling(key,origin,destination):
    """骑行路线规划"""
    cycling_url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    cycling_params = {
        "key":key,
        "origin":origin,
        "destination":destination
    }
    r = requests.get(url=cycling_url,params=cycling_params)
    return r.json()

#货车路径规划
def truck(key,origin,destination,size=2,originid=None,originidtype=None,destinationid=None,destinationtype=None,diu=None,strategy=1,waypoints=None,height=1.6,width=2.5,load=10,weight=0.9,axis=2,province=None,number=None,cartype=0,avoidpolygons=None,showpolyline=1,nosteps=0):
    """货车路线规划"""
    truck_url = "https://restapi.amap.com/v4/direction/truck?parameters"
    truck_params = {
        "key":key,
        "origin":origin,
        "originid":originid,
        "originidtype":originidtype,
        "destination":destination,
        "destinaitonid":destinationid,
        "destinationtype":destinationtype,
        "diu":diu,
        "strategy":strategy,
        "waypoints":waypoints,
        "size":size,
        "height":height,
        "width":width,
        "load":load,
        "weight":weight,
        "axis":axis,
        "province":province,
        "number":number,
        "cartype":cartype,
        "avoidpolygons":avoidpolygons,
        "showpolyline":showpolyline,
        "nosteps":nosteps
    }
    r = requests.get(url=truck_url,params=truck_params)
    return r.json()

#距离测量
def distance(key,origins,destination,type=1,sig=None,output="JSON",callback=None):
    """距离测量"""
    distance_url = "https://restapi.amap.com/v3/distance?parameters"
    distance_params = {
        "key":key,
        "origins":origins,
        "destination":destination,
        "type":type,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r = requests.get(url=distance_url,params=distance_params)
    return r.json()

#行政区域查询
def district(key,keywords=None,subdistrict=None,page=None,offset=None,extensions=None,fitter=None,callback=None,output=None):
    """行政区域查询"""
    district_url = "https://restapi.amap.com/v3/config/district?parameters"
    district_params = {
        "key":key,
        "keywords":keywords,
        "subdistrict":subdistrict,
        "page":page,
        "offset":offset,
        "extensions":extensions,
        "fitter":fitter,
        "callback":callback,
        "output":output,
    }
    r = requests.get(url=district_url,params=district_params)
    return r.json()

# 关键字搜索
def POI(key,keywords,types=None,city=None,citylimit=None,children=None,offset=None,page=None,extensions=None,sig=None,output=None,callback=None):
    """关键字搜索"""
    POI_url = "https://restapi.amap.com/v3/place/text?parameters"
    POI_params = {
    "key":key,
    "keywords":keywords,
    "types":types,
    "city":city,
    "citylimit":citylimit,
    "children":children,
    "offset":offset,
    "page":page,
    "extensions":extensions,
    "sig":sig,
    "output":output,
    "callback":callback,
    }
    r = requests.get(url=POI_url,params=POI_params)
    return r.json()

# 周边搜索
def POIsearch(key,location,keywords=None,types=None,city=None,radius=None,sortrule=None,offset=None,page=None,extensions=None,sig=None,output=None,callback=None):
    """周边搜索"""
    POIsearch_url = "https://restapi.amap.com/v3/place/around?parameters "
    POIsearch_params = {
        "key":key,
        "location":location,
        "keywords":keywords,
        "types":types,
        "city":city,
        "radius":radius,
        "sortrule":sortrule,
        "offset":offset,
        "page":page,
        "extensions":extensions,
        "sig":sig,
        "output":output,
        "callback":callback,
    }
    r = requests.get(url=POIsearch_url,params=POIsearch_params)
    return r.json()

# 多边形搜索
def polygon(key,polygon,keywords=None,types=None,offset=None,page=None,extensions=None,sig=None,output=None,callback=None):
    """多边形搜索"""
    polygon_url = "https://restapi.amap.com/v3/place/polygon?parameters"
    polygon_params = {
        "key":key,
        "polygon":polygon,
        "keywords":keywords,
        "types":types,
        "offset":offset,
        "page":page,
        "extensions":extensions,
        "sig":sig,
        "output":output,
        "callback":callback,
    }
    r = requests.get(url=polygon_url,params=polygon_params)
    return r.json()

# ID查询
def ID(key,ID=None,sig=None,output=None,callback=None):
    """ID查询"""
    ID_url = "https://restapi.amap.com/v3/place/detail?parameters"
    ID_params = {
        "key":key,
        "id":ID,
        "sig":sig,
        "output":output,
        "callback":callback,
    }
    r = requests.get(url=ID_url,params=ID_params)
    return r.json()

# IP定位
def IP(key,ip=None,sig=None,output=None):
    """定位IP所在位置"""
    IP_url = "https://restapi.amap.com/v3/ip?parameters"
    IP_params = {
        "key":key,
        "ip":ip,
        "sig":sig,
        "output":output,
    }
    r = requests.get(url=IP_url,params=IP_params)
    return r.json()

#静态地图
def staticmap(key,location=None,zoom=15,size="400*400",scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None):
    """静态地图"""
    from PIL import Image
    from io import BytesIO
    staticmap_url = "https://restapi.amap.com/v3/staticmap?parameters"
    staticmap_params = {
        "key":key,
        "location":location,
        "zoom":zoom,
        "size":size,
        "scale":scale,
        "markers":markers,
        "labels":labels,
        "paths":paths,
        "traffic":traffic,
        "sig":sig
    }
    r = requests.get(url=staticmap_url,params=staticmap_params)
    map = Image.open(BytesIO(r.content))
    return map

#坐标转换
def convert(key,locations,coordsys="autonavi",sig=None,output="JSON"):
    """坐标转换"""
    convert_url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    convert_params = {
        "key":key,
        "locations":locations,
        "coordsys":coordsys,
        "sig":sig,
        "output":output
    }
    r = requests.get(url=convert_url,params=convert_params)
    return r.json()

#天气查询
#函数
def weather(key,city,extensions="base",output="JSON"):
    """天气查询，默认查询实时天气"""
    weather_url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    weather_params = {
        "key":key,
        "city":city,
        "extensions":extensions,
        "output":output
    }
    r = requests.get(url=weather_url,params=weather_params)
    return r.json()

#输入提示
def assistant(key,keywords,type=None,location=None,city=None,citylimit="false",datatype="all",sig=None,output="JSON",callback=None):
    """输入提示"""
    assistant_url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    assistant_params = {
        "key":key,
        "keywords":keywords,
        "location":location,
        "city":city,
        "citylimit":citylimit,
        "datatype":datatype,
        "sig":sig,
        "output":output,
        "callback":callback,
        "type":type
    }
    r = requests.get(url=assistant_url,params=assistant_params)
    return r.json()

